﻿using System;
using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;

namespace TelecoSystems.Infrastructure.Helpers
{
    
    public class PagedEntityCollectionView<T> : EntityCollectionView<T>, IPagedCollectionView, INotifyPropertyChanged
        where T : Entity, IEditableObject, new()
    {
        private IPagedCollectionView _pagingView;

        public PagedEntityCollectionView(EntitySet<T> entitySet, IPagedCollectionView delegatePagingView)
            : base(entitySet)
        {
            
            
            
            _pagingView = delegatePagingView;

           
            _pagingView.PageChanging += PageChanging;
            _pagingView.PageChanged += PageChanged;

            INotifyPropertyChanged propertyChanged = _pagingView as INotifyPropertyChanged;

            if (propertyChanged != null)
            {
                propertyChanged.PropertyChanged += (s, e) => RaisePropertyChanged(e);
            }
        }

        #region IPagedCollectionView Members

        public bool CanChangePage
        {
            get { return _pagingView.CanChangePage; }
        }

        public bool IsPageChanging
        {
            get { return _pagingView.IsPageChanging; }
        }

        public int ItemCount
        {
            get { return _pagingView.ItemCount; }
        }

        public bool MoveToFirstPage()
        {
            return _pagingView.MoveToFirstPage();
        }

        public bool MoveToLastPage()
        {
            return _pagingView.MoveToLastPage();
        }

        public bool MoveToNextPage()
        {
            return _pagingView.MoveToNextPage();
        }

        public bool MoveToPage(int pageIndex)
        {
            return _pagingView.MoveToPage(pageIndex);
        }

        public bool MoveToPreviousPage()
        {
            return _pagingView.MoveToPreviousPage();
        }

        public event EventHandler<EventArgs> PageChanged = delegate { };
        public event EventHandler<PageChangingEventArgs> PageChanging = delegate { };

        public int PageIndex
        {
            get { return _pagingView.PageIndex; }
        }

        public int PageSize
        {
            get { return _pagingView.PageSize; }
            set { _pagingView.PageSize = value; }
        }

        public int TotalItemCount
        {
            get { return _pagingView.TotalItemCount; }
        }

        #endregion
    }
}
